package com.mth480.rebasedownloader;

import java.util.ArrayList;
import java.util.Date;
import com.biomatters.geneious.publicapi.documents.*;
import com.thoughtworks.xstream.*;
import org.jdom.Element;
import java.util.*;
import java.io.*;

/**
 * Represents all the data needed about a single Restriction Enzyme
 */
public class RestrictionEnzyme implements PluginDocument {
	
	private Date creationDate;
	private String name;
	private String summary;
	private URN urn;
	private ArrayList<String> fields;
	private RestrictionEnzymeData data;
	
	public RestrictionEnzyme() {
		System.out.println("crappy load");
	}
	
	public RestrictionEnzyme(java.io.File file){
		XStream xstream = new XStream();
		RestrictionEnzymeData red = (RestrictionEnzymeData) xstream.fromXML(file.getAbsolutePath());
		LoadData(new Date(), red);
	}
	
	public RestrictionEnzyme(Date creationDate, 
			String name, String summary,
			URN urn, ArrayList<String> fields)
	{
		this.creationDate = creationDate;
		this.name = name;
		this.summary = summary;
		this.urn = urn;
		this.fields = fields;
		
	}
	
	public RestrictionEnzyme(Date creationDate, 
			RestrictionEnzymeData data)
	{
		LoadData(creationDate, data);
	}
	
	private void LoadData(Date creationDate, 
			RestrictionEnzymeData data){
		this.creationDate = creationDate;
		// new URN("JakeAndNick", "doc1.txt", "mth480")
		String filename = "rebaseData/" + data.FileNumber + ".rxml";
		this.urn = new URN("Rebase Downloader", filename, "mth480");
		this.name = data.enzymeName;
		this.summary = "File for restriction enzyme " + data.enzymeName;

		fields = new ArrayList<String>();
		fields.add(data.enzymeName);
		fields.add(data.prototype);
		fields.add(data.microorganism);
		fields.add(data.source);
		fields.add(data.commercialSource);
		this.data = data;
	}
	
	public String getName()
	{
		return name;
	}
	
	public Object getFieldValue(String fieldCodeName)
	{
		for(int i = 0; i < RebaseFields.displayFields.size(); i++)
		{
				DocumentField f = RebaseFields.displayFields.get(i);
				if(f.getCode().equals(fieldCodeName))
				{
					return (Object)fields.get(i);
				}
		}
		return new Object();
	}
	
	public List<DocumentField> getDisplayableFields()
	{
		return RebaseFields.displayFields;
	}
	
	public String getSummary()
	{
		return summary;
	}
	
	public URN getURN()
	{
		return urn;
	}
	
	public String toHTML()
	{
	//	String filename = this.urn.assigner;
	//	return "<html><body>" + filename + "</body></html>";
		
		return "<html><head><title>Enzyme: " + data.enzymeName + "</title></head>" +
				"<body>" + 
				"<p><b>Enzyme Name: </b>" + data.enzymeName + "</p>" +
				"<p><b>Prototype: </b>" + data.prototype + "</p>" +
				"<p><b>Microorganism: </b>" + data.microorganism + "</p>" +
				"<p><b>Source: </b>" + data.source + "</p>" +
				"<p><b>Commercial Source: </b>" + data.commercialSource + "</p>" +
				"<p><b>Recognition Sequence: </b><blink>" + data.recognitionSequence+ "</blink></p>" +
				"<p><b>Methylation Site AndType: </b>" + data.methylationSiteAndType + "</p>" +
				"</body></html>";
				
	}
	
	public Date creationDate()
	{
		return creationDate;
	}
	
    public Element toXML() {  
        Element root = new Element("RebaseDataz");  
 
        root.addContent(new Element("name").setText(name));  
        root.addContent(new Element("date").setText("" + creationDate.getTime()));  
        root.addContent(new Element("dataFileLocation").setText(urn.assigner));         
        return root;  
    }  
	/**
	 * reads data from xml into object
	 */
    public void fromXML(Element doc) {  
        name = doc.getChildText("name");  
        final String dateText = doc.getChildText("date");  
        try {  
        	creationDate = new Date(Long.parseLong(dateText));  
        } catch (NumberFormatException e) {  
            // should not happen  
        }  
        String fileLocation = doc.getChildText("dataFileLocation");
        XStream xstream = new XStream();
        
        try {
            final Reader reader = new FileReader(fileLocation);
            System.out.println(readerToString(reader));
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        try{
        	BufferedReader in = new BufferedReader(new FileReader(fileLocation));
    		RestrictionEnzymeData red = (RestrictionEnzymeData) xstream.fromXML(in);
    		LoadData(new Date(), red);
        }catch(FileNotFoundException fnf){ fnf.printStackTrace(); }
    }
    
    
    static String readerToString(final Reader reader) throws IOException {
        final StringBuilder sb = new StringBuilder();
        while( true ) {
            int c = reader.read();
            if( c < 0 ) return sb.toString();

            sb.append((char)c);
        }
    }
}
